package com.hgdp.medicalexamination.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgdp.medicalexamination.entity.Enclosure;
import com.hgdp.medicalexamination.service.EnclosureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.UUID;

/**
 * @param
 * @author hgdp
 * @date 2020-07-31 09:28:21
 */
@RestController
@AllArgsConstructor
@Api(value = "操作接口")
@RequestMapping("/enclosure")
public class EnclosureController {

    private EnclosureService enclosureService;

    /**
     * 列表
     */
    @ApiOperation(value = "分页查询", notes = "分页查询enclosure ")
    @GetMapping("/pagelist")
    public R list(Page page, Enclosure enclosure) {
        return R.ok(enclosureService.queryPage(page, enclosure));
    }

    /**
     * 信息
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询enclosure ")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
        return R.ok(enclosureService.getById(id));
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存", notes = "保存")
    @PostMapping("/save")
    public R save(@RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
        if (file == null) {
            return R.ok(false);
        }
        Enclosure enclosure = new Enclosure();
        String originalFilename = file.getOriginalFilename();
        if (originalFilename.length()>50){
            return R.ok("文件名太长");
        }
        String id = UUID.randomUUID().toString();
        enclosure.setId(id);
        enclosure.setEnclosureFile(file.getBytes());
        enclosure.setFileName(originalFilename);
        enclosureService.save(enclosure);
        return R.ok(id);
    }

    /**
     * 获取
     */
    @ApiOperation(value = "获取", notes = "获取")
    @PostMapping("/download/{id}")
    public void download(@PathVariable String id, HttpServletResponse response)  {
        Enclosure enclosure = enclosureService.getById(id);
        if (enclosure!=null){
            byte[] enclosureFile = enclosure.getEnclosureFile();
            if (enclosureFile.length>0){
                try {
                    response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(enclosure.getFileName(),"UTF-8"));
                    OutputStream outputStream = response.getOutputStream();
                    outputStream.write(enclosureFile);
                    outputStream.flush();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 修改
     *
     * @param
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @PostMapping("/update")
    public R update(@RequestParam(value = "file", required = false) MultipartFile file,@RequestParam(value = "id", required = false) String id) {
        if (file == null) {
            return R.ok(false);
        }
        Enclosure enclosure = new Enclosure();
        String originalFilename = file.getOriginalFilename();
        if (originalFilename.length()>50){
            return R.ok("文件名太长");
        }
        enclosure.setId(id);
        try {
            enclosure.setEnclosureFile(file.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        enclosure.setFileName(originalFilename);
        return R.ok(enclosureService.updateById(enclosure));
    }

    /**
     * 删除
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @DeleteMapping("/delete")
    public R delete(@RequestBody String[] ids) {
        return R.ok(enclosureService.removeByIds(Arrays.asList(ids)));
    }
}